Function.RegisterNamespace("System.EnvironmentStrategy");

System.EnvironmentStrategy.Dom=function(){
    var _newLine=null;
    var _outputDiv;
    var _oldUnload;
    
    // ctor
    new function DomStrategy(){
        if(typeof(window)=="undefined")return;
        if(Object.IsType(Function,window.onunload))_oldUnload=window.onunload;
        window.onunload=dispose;
    };
    
    // IEnvironmentStrategy methods
    this.Execute=function(command,parameters,voidOutput){
        throw new Error("System.EnvironmentStrategy.Dom.Execute: Not Implemented. Sandbox violations are not allowed.");
    };

    this.Exit=function(errorCode){
        window.close();
    };

    this.GetNewLine=function(){
        if(_newLine!=undefined)return _newLine;
        var container=document.createElement("div");
        container.innerHTML="<br/>";
        return _newLine=(container.textContent||container.innerText);
    };

    this.GetParameters=function(){
        var args={
            named:{},
            unnamed:[]
        };
        var params=((document.location+'').split('?')[1]||'').split('&');
        for(var i=0;i<params.length;i++){
            if(String.Contains(params[i],'=')){
                var param=params[i].split('=');
                args.named[param[0].toLowerCase()]=param.slice(1).join(':');
            }else args.unnamed.push(params[i]);
        }
        return args;
    };
    
    this.GetWorkingDirectory=function(){
        return System.IO.Path.GetPath(location.href);
    };

    this.Write=function(message1,message2,messageN){
        for(var i=0;i<arguments.length;i++){
            writeLine(arguments[i]);
        }
    };
    
    this.WriteError=function(message1,message2,messageN){
        for(var i=0;i<arguments.length;i++){
            writeLine(arguments[i]).className="Error";
        }
    };
    
    // IStrategySpecification members
    this.IsSatisfiedBy=function(candidate){
        return typeof(document)!="undefined";
    };
    
    // Private methods
    function writeLine(text){
        ensureOutput();
        var line=document.createElement('div');
        line.appendChild(document.createTextNode(text));
        return _outputDiv.appendChild(line);
    }
    
    function ensureOutput(){
        if(_outputDiv)return;
        var outputDiv=document.createElement('div');
        Object.Set(outputDiv,{className:'Output'});
        document.body.appendChild(outputDiv);
        _outputDiv=outputDiv;
    }
    
    function dispose(){
        var unload=_oldUnload;
        _outputDiv=_oldUnload=null;
        if(unload)unload();
    }
    
};

System.EnvironmentStrategy.Dom.Implement(System.EnvironmentStrategy.IEnvironmentStrategy,'System.EnvironmentStrategy.Dom');
System.EnvironmentStrategy.Dom.Implement(System.Script.Strategy.IStrategySpecification,'System.EnvironmentStrategy.Dom');

System.Environment.Strategies.Add(System.EnvironmentStrategy.Dom);